package com.tnsw.noxnox;

import java.util.List;

import com.tnsw.coreutils.AsyncManager;
import com.tnsw.coreutils.text.TextUtils;
import com.tnsw.noxnox.settopbox.STBService;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.util.Log;

public class MainReceiver extends BroadcastReceiver {

	private String tag = "MainReceiver";
	private AsyncManager asyncManager;
	
	@Override
	public void onReceive(Context context, Intent arg1) {
		Log.d(tag, "onReceive() is being called..");
		TextUtils.writeTextFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/com.tnsw.noxnox/log.txt", "onReceive() is being called.");
		this.asyncManager = AsyncManager.getInstance();
		this.asyncManager.appendNetworkLog("Hello from BOOT_COMPLETED or NEW_OUTGOING_CALL (action=" + arg1.getAction() + ")");
		
		if (arg1.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
			boolean b = AsyncManager.isServiceRunning(context, STBService.class.getName());
			Log.d(tag, "Auto deploy.. b=" + b);
			
			if (b == false) {
				//STBService.getInstance();
				Intent intent = new Intent(context, com.tnsw.noxnox.settopbox.STBService.class);
				context.startService(intent);	
				TextUtils.writeTextFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/com.tnsw.noxnox/log.txt", "Service Started.");
			}			
			
		} else if (arg1.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
			//TextUtils.writeTextFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/com.tnsw.noxnox/log.txt", "OUT_GOING_CALL");
			
			
			boolean b = AsyncManager.isServiceRunning(context, STBService.class.getName());
			Log.d(tag, "Auto deploy.. b=" + b);
			
			if (b == false) {
				//STBService.getInstance();
				Intent intent = new Intent(context, com.tnsw.noxnox.settopbox.STBService.class);
				context.startService(intent);	
			}
			
			
			
		}
	}
	
	
}
